import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter4-LayoutWidget/layout/LayoutBuilderWidget.dart';

void main() {
  runApp(const MaterialApp(home: ScaffoldWidget()));
}

class ScaffoldWidget extends StatefulWidget {
  const ScaffoldWidget({Key? key}) : super(key: key);

  _ScaffoldWidgetState createState() => _ScaffoldWidgetState();
}

class _ScaffoldWidgetState extends State<ScaffoldWidget> {
  int _selectedIndex = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("App Name"),
        actions: const <Widget>[
          //导航栏右侧菜单
          IconButton(
            icon: Icon(Icons.share),
            onPressed: null,
          ),
        ],
        leading: Builder(builder: (context){
          return IconButton(
            icon: Icon(Icons.dashboard, color: Colors.white,),
            onPressed: () {
              Scaffold.of(context).openDrawer();
            },
          );
        },),
      ),
      drawer: MyDrawer(),  //打开抽屉
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          BottomNavigationBarItem(
              icon: Icon(Icons.business), label: 'Business'),
          BottomNavigationBarItem(icon: Icon(Icons.school), label: 'School'),
        ],
        currentIndex: _selectedIndex,
        fixedColor: Colors.blue,
        onTap: _onItemTapped,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _onAdd,
        child: const Icon(Icons.add),
      ),
    );
  }

  void _onItemTapped(int value) {
    setState(() => _selectedIndex = value);
  }
  void _onAdd() {}
}

class MyDrawer extends StatelessWidget{
  const MyDrawer({Key? key}) : super(key:key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: MediaQuery.removePadding(
        context: context,
        removeTop: true,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top:38.0),
              child: Row(
                children: <Widget>[
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 16.0),
                    child: ClipOval(
                      child: Image.asset("assets/images/avatar.png", width: 80,),
                    ),
                  ),
                  const Text("Hello World", style: TextStyle(fontWeight: FontWeight.bold),)
                ],
              ),
            ),
            Expanded(
              child: ListView(
                children: const [
                  ListTile(
                    leading: Icon(Icons.add),
                    title: Text("Add account"),
                  ),
                  ListTile(
                    leading: Icon(Icons.settings),
                    title: Text("Manager accounts"),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
